package com.lanou.mynews.news;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.mynews.R;
import com.lanou.mynews.dao.Person;
import com.lanou.mynews.dao.PersonDao;
import com.lanou.mynews.main.InitApplication;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 16/1/11.
 */
public class NewsAdapter extends BaseAdapter {

    private List<NewEty.T1348647909107Entity> datas;
    private Context context;


    public NewsAdapter(List<NewEty.T1348647909107Entity> datas, Context mContext) {
        this.datas = datas;
        this.context = mContext;

    }

    @Override
    public int getCount() {
        if (datas != null && datas.size() > 0) {
            return datas.size();
        }

        return 0;
    }

    @Override
    public NewEty.T1348647909107Entity getItem(int position) {
        if (datas != null && datas.size() > 0)
            return datas.get(position);

        else

            return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public int getItemViewType(int position) {
        boolean flag = datas.get(position).getImqnewextra() == null;
        boolean a = datas.get(position).getImgType() == 1;

        if (a) {
            return 2;
        } else if (flag) {
            return 0;

        } else {
            return 1;
        }


    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    //加载方法
     public void refreshData(List<NewEty.T1348647909107Entity> datas){
         this.datas = datas;
         notifyDataSetChanged();



     }



    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        ViewHoldert holdert = null;
        ViewHolderthree holderthree = null;

        if (view == null) {
            switch (getItemViewType(position)) {
                case 0:
                    view = LayoutInflater.from(context).inflate(R.layout.news_listview, null);
                    holder = new ViewHolder();
                    holder.imageview = (NetworkImageView) view.findViewById(R.id.news_iv);
                    holder.textView = (TextView) view.findViewById(R.id.news_tv);
                    holder.textViewtwo = (TextView) view.findViewById(R.id.news_tvt);
                    view.setTag(holder);
                    break;
                case 1:


                    view = LayoutInflater.from(context).inflate(R.layout.news_listview_two, null);
                    holdert = new ViewHoldert();
                    holdert.textView = (TextView) view.findViewById(R.id.newst_tv);
                    holdert.imageViewt = (NetworkImageView) view.findViewById(R.id.two1);
                    holdert.imageViewone = (NetworkImageView) view.findViewById(R.id.two2);
                    holdert.imageViewtwo = (NetworkImageView) view.findViewById(R.id.two3);
                    view.setTag(holdert);
                    break;
                case 2:
                    view = LayoutInflater.from(context).inflate(R.layout.news_listview_three, null);
                    holderthree = new ViewHolderthree();
                    holderthree.textView = (TextView) view.findViewById(R.id.news_three_tv);
                    holderthree.imageView = (NetworkImageView) view.findViewById(R.id.news_three_iv);
                    view.setTag(holderthree);
                    break;
            }


        } else {
            switch (getItemViewType(position)) {
                case 0:
                    holder = (ViewHolder) view.getTag();
                    break;
                case 1:
                    holdert = (ViewHoldert) view.getTag();
                    break;
                case 2:
                    holderthree = (ViewHolderthree) view.getTag();
                    break;
            }

        }
        NewEty.T1348647909107Entity newety = getItem(position);
        if (newety != null) {

            switch (getItemViewType(position)) {
                case 0:
                    holder.textView.setText(newety.getTitle());
                    holder.textViewtwo.setText(newety.getDigest());

                    //数据库增删改查
                    Person data2 = new Person();
                    data2.setName(newety.getTitle());
                    InitApplication.daoSession.getPersonDao().insert(data2);
                    InitApplication.daoSession.deleteAll(Person.class);

                    QueryBuilder qb = InitApplication.daoSession.getPersonDao().queryBuilder();
                    qb.where(PersonDao.Properties.Name.eq(""));
                    Person da = (Person) qb.unique();

//                    holder.imageview.setDefaultImageResId(R.mipmap.ic_launcher);
//                    holder.imageview.setErrorImageResId(R.mipmap.ic_launcher);
                    RequestQueue queue = Volley.newRequestQueue(context);
                    ImageLoader imageloader = new ImageLoader(queue, new ImageLoader.ImageCache() {
                        @Override
                        public Bitmap getBitmap(String url) {
                            return null;
                        }

                        @Override
                        public void putBitmap(String url, Bitmap bitmap) {

                        }
                    });
                    holder.imageview.setImageUrl(newety.getImg(), imageloader);
                    break;
                case 1:
                    holdert.textView.setText(newety.getTitle());

                    holdert.imageViewone.setDefaultImageResId(R.drawable.loading);
                    holdert.imageViewone.setErrorImageResId(R.drawable.loading);
                    RequestQueue queuet = Volley.newRequestQueue(context);
                    ImageLoader imageloadert = new ImageLoader(queuet, new ImageLoader.ImageCache() {
                        @Override
                        public Bitmap getBitmap(String url) {
                            return null;
                        }

                        @Override
                        public void putBitmap(String url, Bitmap bitmap) {

                        }
                    });
                    holdert.imageViewt.setImageUrl(newety.getImg(), imageloadert);
                    holdert.imageViewone.setImageUrl(newety.getImqnewextra().get(0).getImgsrc(), imageloadert);
                    holdert.imageViewtwo.setImageUrl(newety.getImqnewextra().get(1).getImgsrc(), imageloadert);
                    break;
                case 2:
                    holderthree.textView.setText(newety.getTitle());

                    holderthree.imageView.setDefaultImageResId(R.drawable.loading);
                    holderthree.imageView.setErrorImageResId(R.drawable.loading);
                    RequestQueue queuett = Volley.newRequestQueue(context);
                    ImageLoader imageloadertt = new ImageLoader(queuett, new ImageLoader.ImageCache() {
                        @Override
                        public Bitmap getBitmap(String url) {
                            return null;
                        }

                        @Override
                        public void putBitmap(String url, Bitmap bitmap) {

                        }
                    });
                    holderthree.imageView.setImageUrl(newety.getImg(), imageloadertt);
                    break;


            }



        }

        return view;

    }

    private class ViewHolder {

        public NetworkImageView imageview;
        public TextView textView, textViewtwo;


    }

    private class ViewHoldert {
        public TextView textView;
        public NetworkImageView imageViewt, imageViewone, imageViewtwo;


    }

    private class ViewHolderthree {
        public TextView textView;
        public NetworkImageView imageView;


    }


}
